package com.jk.vehicle.core.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jk.vehicle.core.exception.ServiceException;
import com.jk.vehicle.core.service.Service;
import com.jk.vehicle.core.entity.BaseEntity;
import com.jk.vehicle.core.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * 基于通用MyBatis Mapper插件的Service接口的实现
 *
 * @author KangJian
 * @date 2018/8/7
 */

public abstract class AbstractService<T extends BaseEntity> implements Service<T> {

	/**
	 * 通过mapper
	 */
	@Autowired
	protected Mapper<T> mapper;

	/**
	 * 当前泛型真实类型的Class
	 */
	private Class<T> modelClass;

	/**
	 * 删除标识
	 */
	private static final Byte DEL_FLAG = 1;

	/**
	 * 逻辑删除失败提示信息
	 */
	private static final String DELETE_ERROR_MSG = "删除出错";

	public AbstractService() {
		ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
		modelClass = (Class<T>) pt.getActualTypeArguments()[0];
	}

	@Override
	@DS("#last")
	public void save(T model, String dsName) {
		mapper.insertSelective(model);
	}

	@Override
	@DS("#last")
	public void save(List<T> models, String dsName) {
		mapper.insertList(models);
	}

	@Override
	@DS("#last")
	public void deleteById(Long id, String dsName) {
		try {
			T model = modelClass.newInstance();
			model.setDelFlag(DEL_FLAG);
			model.setId(id);
			mapper.updateByPrimaryKeySelective(model);
		} catch (InstantiationException | IllegalAccessException e) {
			throw new ServiceException(DELETE_ERROR_MSG + e.getMessage());
		}
	}

	@Override
	@DS("#last")
	public void deleteByIds(String ids, String dsName) {
		mapper.deleteByIds(ids);
	}

	@Override
	@DS("#last")
	public void deleteByIdPermanent(Long id, String dsName) {
		mapper.deleteByPrimaryKey(id);
	}

	@Override
	@DS("#last")
	public void deleteByIdsPermanent(String ids, String dsName) {
		mapper.deleteByIds(ids);
	}

	@Override
	@DS("#last")
	public void update(T model, String dsName) {
		T modelOld = mapper.selectByPrimaryKey(model.getId());
		if (modelOld == null) {
			throw new ServiceException("更新失败，对象不存在");
		}
		mapper.updateByPrimaryKeySelective(model);
	}

	@Override
	@DS("#last")
	public void updateStrict(T model, String dsName) {
		T modelOld = mapper.selectByPrimaryKey(model.getId());
		if (modelOld == null) {
			throw new ServiceException("更新失败，对象不存在");
		}
		mapper.updateByPrimaryKey(model);
	}

	@Override
	@DS("#last")
	public T findById(Long id, String dsName) {
		T model = mapper.selectByPrimaryKey(id);
			return model;
	}

	@Override
	@DS("#last")
	public List<T> findByIds(String ids, String dsName) {
		List<T> models = mapper.selectByIds(ids);
		return models;
	}

	@Override
	@DS("#last")
	public List<T> findByCondition(Condition condition, String dsName) {
		if (condition == null) {
			throw new ServiceException("查询失败，查询条件不能为空");
		}
		return mapper.selectByCondition(condition);
	}

	@Override
	@DS("#last")
	public Integer countByCondition(Condition condition, String dsName) {
		if (condition == null) {
			throw new ServiceException("查询失败，查询条件不能为空");
		}
		return mapper.selectCountByCondition(condition);
	}

	@Override
	@DS("#last")
	public PageInfo page(Integer page, Integer size, Condition condition, String dsName) {
		page = page == null ? 1 : page;
		size = size == null ? 10 : size;
		PageHelper.startPage(page, size);
		return new PageInfo(mapper.selectByCondition(condition));
	}

	@Override
	@DS("#last")
	public void deleteByCondition(Condition condition, String dsName) {
		if (condition == null) {
			throw new ServiceException("查询失败，查询条件不能为空");
		}
		mapper.deleteByCondition(condition);
	}
}
